package Algorithms.jishi.niuke.huawei;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

/**
 * description： <br>
 * createTime: 2020/11/1411:23 <br>
 *
 * @author zzh
 */
public class Test24Points {
    public static int TOTAL = 4;
    public static Operate[] OPERATE_ARRAY = Operate.values();
    public static int[] convertStrArray2IntArray (String[] array) {
        int[] values = new int[TOTAL];
        for(int i = 0; i < TOTAL; i++) {
            String s = array[i];
            switch(s) {
                case "A":
                    values[i] = 1;
                    break;
                case "2":
                    values[i] = 2;
                    break;
                case "3":
                    values[i] = 3;
                    break;
                case "4":
                    values[i] = 4;
                    break;
                case "5":
                    values[i] = 5;
                    break;
                case "6":
                    values[i] = 6;
                    break;
                case "7":
                    values[i] = 7;
                    break;
                case "8":
                    values[i] = 8;
                    break;
                case "9":
                    values[i] = 9;
                    break;
                case "10":
                    values[i] = 10;
                    break;
                case "J":
                    values[i] = 11;
                    break;
                case "Q":
                    values[i] = 12;
                    break;
                case "K":
                    values[i] = 13;
                    break;
                case "joker":
                case "JOKER":
                default:
                    System.out.println("ERROR");
                    System.exit(0);
            }
        }
        return values;
    }

    public static List<Set<Integer>> getAllDiffCombination() {
        List<Set<Integer>> result = new ArrayList<>();
        addCominationRecurse(result, null);
        return result;
    }

    public static List<List<Integer>> getAllCombination() {
        List<List<Integer>> result = new ArrayList<>();
        addOperateRecurse(result, null);
        return result;
    }

    private static void addOperateRecurse(List<List<Integer>> result, List<Integer> list) {
        for (int i = 0; i < TOTAL; i++) {
            if(list == null) {
                list = new ArrayList<>(TOTAL - 1);
            } else if (list.size() == TOTAL - 1) {
                result.add(list);
                return;
            }
            List<Integer> newList = new ArrayList<>();
            newList.addAll(list);
            list.add(i);
            addOperateRecurse(result, list);
            list = newList;
        }
    }


    private static void addCominationRecurse(List<Set<Integer>> result, Set<Integer> set) {
        for (int i = 0; i < TOTAL; i++) {
            if(set == null) {
                set = new LinkedHashSet<>();
            } else if(set.size() == TOTAL) {
                result.add(set);
                return;
            }
            Set<Integer> newSet = new LinkedHashSet<>();
            newSet.addAll(set);
            if(set.contains(i)) {
                continue;
            } else {
                set.add(i);
                addCominationRecurse(result, set);
            }
            set = newSet;
        }
    }


    public static String getResult(int[] values) {
        List<Set<Integer>> allDiffCombination = getAllDiffCombination();
        List<List<Integer>> allCombination = getAllCombination();
        for (Set<Integer> integers : allDiffCombination) {
            for (List<Integer> list : allCombination) {
                int count = 0;
                Float result = null;
                StringBuilder sb = new StringBuilder();
                Iterator<Integer> iterator = integers.iterator();
                while(count++ < TOTAL && iterator.hasNext()) {
                    Integer nextIndex4Value = iterator.next();
                    int value = values[nextIndex4Value];
                    if(result == null) {
                        result = value * 1.0F;
                    } else {
                        Integer nextIndex4Operate = list.get(count - 2);
                        Operate operate = OPERATE_ARRAY[nextIndex4Operate];
                        sb.append(operate.getOperateStr());
                        result = operate.operate(result, value);
                    }
                    sb.append(value);
                }
                if(Math.abs(result - 24) < 0.01) {
                    return sb.toString();
                }
            }
        }
        return "NONE";
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] array = new String[4];
        for(int i = 0; i < TOTAL; i++) {
            array[i] = scanner.next();
        }

        int[] values = convertStrArray2IntArray(array);
        String result = getResult(values);
        System.out.println(result);
    }

    enum Operate{
        PLUS("+"){
            public float operate(float a, float b) {
                return a + b;
            }
        }, MINUS("-"){
            public float operate(float a, float b) {
                return a - b;
            }
        }, MULTIPLY("*"){
            public float operate(float a, float b) {
                return a * b;
            }
        }, DIVIDE("/"){
            public float operate(float a, float b) {
                return a / b;
            }

        };
        Operate(String operateStr) {
            this.operateStr = operateStr;
        }
        private String operateStr;
        public abstract float operate(float a, float b);


        public String getOperateStr() {
            return operateStr;
        }
    }
}
